1   /**
2    * Copyright 2014 Netflix, Inc.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5    * compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is
10   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11   * the License for the specific language governing permissions and limitations under the License.
12   */
13  package rx.internal.util;
14  
15  import rx.functions.Func0;
16  import rx.functions.Func1;
17  import rx.functions.Func2;
18  import rx.functions.Func3;
19  import rx.functions.Func4;
20  import rx.functions.Func5;
21  import rx.functions.Func6;
22  import rx.functions.Func7;
23  import rx.functions.Func8;
24  import rx.functions.Func9;
25  import rx.functions.FuncN;
26  
27  /**
28   * Utility functions for internal use that we don't want part of the public API. 
29   */
30  public final class UtilityFunctions {
31  
32      /**
33       * Returns a function that always returns {@code true}.
34       *
35       * @return a {@link Func1} that accepts an Object and returns the Boolean {@code true}
36       */
37      public static <T> Func1<? super T, Boolean> alwaysTrue() {
38          return AlwaysTrue.INSTANCE;
39      }
40  
41      /**
42       * Returns a function that always returns {@code false}.
43       *
44       * @return a {@link Func1} that accepts an Object and returns the Boolean {@code false}
45       */
46      public static <T> Func1<? super T, Boolean> alwaysFalse() {
47          return AlwaysFalse.INSTANCE;
48      }
49  
50      /**
51       * Returns a function that always returns the Object it is passed.
52       *
53       * @return a {@link Func1} that accepts an Object and returns the same Object
54       */
55      public static <T> Func1<T, T> identity() {
56          return new Func1<T, T>() {
57              @Override
58              public T call(T o) {
59                  return o;
60              }
61          };
62      }
63  
64      private enum AlwaysTrue implements Func1<Object, Boolean> {
65          INSTANCE;
66  
67          @Override
68          public Boolean call(Object o) {
69              return true;
70          }
71      }
72  
73      private enum AlwaysFalse implements Func1<Object, Boolean> {
74          INSTANCE;
75  
76          @Override
77          public Boolean call(Object o) {
78              return false;
79          }
80      }
81  
82      /**
83       * Returns a function that merely returns {@code null}, without side effects.
84       *
85       * @return a function that returns {@code null}
86       */
87      @SuppressWarnings("unchecked")
88      public static <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, R> NullFunction<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, R> returnNull() {
89          return NULL_FUNCTION;
90      }
91  
92      @SuppressWarnings("rawtypes")
93      private static final NullFunction NULL_FUNCTION = new NullFunction();
94  
95      private static final class NullFunction<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, R> implements
96              Func0<R>,
97              Func1<T0, R>,
98              Func2<T0, T1, R>,
99              Func3<T0, T1, T2, R>,
100             Func4<T0, T1, T2, T3, R>,
101             Func5<T0, T1, T2, T3, T4, R>,
102             Func6<T0, T1, T2, T3, T4, T5, R>,
103             Func7<T0, T1, T2, T3, T4, T5, T6, R>,
104             Func8<T0, T1, T2, T3, T4, T5, T6, T7, R>,
105             Func9<T0, T1, T2, T3, T4, T5, T6, T7, T8, R>,
106             FuncN<R> {
107         @Override
108         public R call() {
109             return null;
110         }
111 
112         @Override
113         public R call(T0 t1) {
114             return null;
115         }
116 
117         @Override
118         public R call(T0 t1, T1 t2) {
119             return null;
120         }
121 
122         @Override
123         public R call(T0 t1, T1 t2, T2 t3) {
124             return null;
125         }
126 
127         @Override
128         public R call(T0 t1, T1 t2, T2 t3, T3 t4) {
129             return null;
130         }
131 
132         @Override
133         public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5) {
134             return null;
135         }
136 
137         @Override
138         public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6) {
139             return null;
140         }
141 
142         @Override
143         public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6, T6 t7) {
144             return null;
145         }
146 
147         @Override
148         public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6, T6 t7, T7 t8) {
149             return null;
150         }
151 
152         @Override
153         public R call(T0 t1, T1 t2, T2 t3, T3 t4, T4 t5, T5 t6, T6 t7, T7 t8, T8 t9) {
154             return null;
155         }
156 
157         @Override
158         public R call(Object... args) {
159             return null;
160         }
161     }
162     
163 }